package org.mnj07.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SwingExample implements Runnable {
  @Override
  public void run() {
      // Create the window
      JFrame f = new JFrame ("Hello, World!");
      // Sets the behavior for when the window is closed
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // add a label and a button
      JPanel panel = new JPanel();
      panel.setLayout(new FlowLayout(FlowLayout.CENTER));
      panel.add(new JLabel("Hello, world!"));
      JButton button = new JButton("Press me!");
      panel.add(button);
      
      button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
          System.out.println("Button pressed: " + e);
        }
      });
      
      f.getContentPane().add(panel);
      // arrange the components inside the window
      f.pack();
      //By default, the window is not visible. Make it visible.
      f.setVisible(true);
  }

  public static void main(String[] args) {
      SwingExample se = new SwingExample();
      // Schedules the application to be run at the correct time in the event queue.
      SwingUtilities.invokeLater(se);
  }
}
